package com.jiang.action;

import java.io.IOException;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.jiang.service.IMessageService;
import com.jiang.vo.Message;
import com.jiang.vo.Type;

@Controller
@RequestMapping("/pages/back/message")
public class MessageAction extends BaseAction{

	public String insert_rule = "mid:int|title:String";
	
	@Autowired
	private IMessageService messageService;
	
	@RequestMapping(value= "/hello_demo", method=RequestMethod.GET)
	public void demo(Message message){
		System.out.println(message);
		System.out.println("我是蒋正午");
		return ;
	}
	
	@RequestMapping("/go")
	public ModelAndView demo2Forward(Message message){
		// 注意这里要服务器端跳转了，要用绝对路径直接跳转到根路径而不是还在相对路径上玩
		ModelAndView mv = new ModelAndView("forward"); //后台路由这里就会自动找到安全路径下
		mv.addObject("msg", "跳转成功");
		mv.addObject("url", "index.jsp");
		return mv;
	}
	
	@RequestMapping(value="/insert")
	public ModelAndView insert(Message msg, MultipartFile pic){
		// 记住配置文件的编码不要错了 一定要UTF-8
		String msginfo = messageSource.getMessage("info", new Object[]{},Locale.getDefault());
		System.out.println(msginfo);
		messageService.insert(msg);
		// 如果不想出现404, 那么不跳转的话就还是返回ModelAndView类型但是返回值为null
		System.out.println(pic.isEmpty());
		System.out.println(pic.getName());
		System.out.println(pic.getContentType());
		System.out.println(pic.getSize());
		return null;
	}
	
	@RequestMapping("/update")
	public ModelAndView update(Message msg){
		messageService.update(msg);
		return null;
	}
	
	@RequestMapping("/update2")
	public ModelAndView update(Message msg, Type type, HttpServletRequest request, HttpServletResponse response){
		msg.setType(type);
		messageService.update(msg);
		System.out.println(request);
		System.out.println(response);
		try {
			response.setCharacterEncoding("UTF-8");
			response.getWriter().println("<h1>你好</h1>");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping("/get")
	public ModelAndView get(@RequestParam(value="myid",defaultValue="1") int id){
		System.out.println(messageService.get(id));
		return null;
	}
	
	@RequestMapping("/list")
	public ModelAndView list(@RequestParam(value="column",defaultValue="title") String column,
														    @RequestParam(value="keyWord",defaultValue="jzw")String keyWord,
														    @RequestParam(value="currentPage",defaultValue="1")int currentPage,
														    @RequestParam(value="lineSize",defaultValue="5")int lineSize){
		System.out.println(column+":" + keyWord+":"+currentPage+":"+lineSize);
		System.out.println(messageService.list(column, keyWord, currentPage, lineSize));
		return null;
	}
	
	
}
